package pers.mine.server.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.mine.jo.Log;
import pers.mine.mapper.LogMapper;
import pers.mine.server.ILogService;

import java.util.List;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    @Transactional
    public boolean saveBatchMine(List<Log> logs) {
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        try {
            LogMapper mapper = session.getMapper(LogMapper.class);
            for (Log log : logs) {
                mapper.insertTest(log);
            }
            session.flushStatements();
            session.commit(true);
        } catch (Exception e) {
            session.rollback(true);
            throw e;
        } finally {
            session.clearCache();
            session.close();
        }
        return true;
    }

    @Override
    @Transactional
    public boolean saveBatchMineDynamicSql(List<Log> logs) {
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        try {
            LogMapper mapper = session.getMapper(LogMapper.class);
            for (Log log : logs) {
                mapper.insertTestDynamicSql(log);
            }
            session.flushStatements();
            session.commit(true);
        } catch (Exception e) {
            session.rollback(true);
            throw e;
        } finally {
            session.clearCache();
            session.close();
        }
        return true;
    }
}
